<template>
  <div class="container">
    <div class="exchange-wrapper">
      <div v-if="forumCon.length > 0">
      <van-list
        v-model="loading"
        :finished="finished"
        :immediate-check="false"
        finished-text="没有更多了"
        @load="onLoad"
      >
      <div class="forum-item" v-for="item in forumCon" @click="toDetail(item)">
        <div class="forum-item-top">
          <p>{{item.title}}</p>
        </div>
        <div class="forum-item-bottom">
          <img v-if="!item.pic" class="user-thumb" src="@/assets/images/userimg.png"/>
          <img  class="user-thumb" v-else :src="item.pic"/>
          <span class="user-text">{{item.poster}}</span>
          <span class="create-time">{{item.posttime}}</span>
        </div>
      </div>
      </van-list>
      </div>
      <div v-else class="nodata">--- 暂无数据 ---</div>

      <!--<div class="add-wrapper" @click="editor"><van-icon name="add" color="#1989fa"  /></div>-->
      <div class="btn-wrapper">
        <button class="exam-btn" @click="editor">发表问题</button>
      </div>
    </div>

    <van-dialog
      v-model="show"
      title="编辑评论"
      :beforeClose="beforeClose"
      show-cancel-button
      confirm-button-text="发表"
    >
      <div class="container">
        <van-field
          v-model="title"
          name="title"
          label="标题"
          placeholder="请填写标题"
          :rules="[{ required: true, message: '请填写标题' }]"
        />

        <van-field
          v-model="content"
          name="content"
          rows="3"
          autosize
          label=""
          type="textarea"
          placeholder="请输入内容"
        />

      </div>
    </van-dialog>

  </div>
</template>

<script>
  import util from '@/common/util';
  export default {
    data() {
      return {
        show: false,
        page: 1,
        forumCon: [],
        baseURL: '',
        title: '',
        content: '',
        pic: '',
        stname: '',
        loading: false,
        finished: false,
      }
    },
    created() {
      let that = this;
      that.baseURL = util.baseURL;
      if(util.storage.get('userinfo')){
        this.pic = util.storage.get('userinfo').pic;
        this.stname = util.storage.get('userinfo').stname;
      }
      that.getForumList();
    },

    methods: {
      getForumList() {
        let that = this;
        that.$toast.loading({
          message: '加载中...'
        });
        that.$api.post('/mobile/Stmsg/checklt',{"user":{"page": that.page}}, r => {
          //console.log('r',r)
          let rows = r.data; //请求返回当页的列表
          that.$toast.clear();
          console.log('rows',rows);
          that.loading = false;
          if (rows == null || rows.length === 0) {
            this.finished = true; // 加载结束
            return;
          }
          that.forumCon = that.forumCon.concat(rows);
          console.log('forumCon',that.forumCon)
        })
      },

      onLoad() { //下拉加载更多
        this.page++;
        this.getForumList();
      },

      toDetail(item) {
        let that = this;
        console.log(item);
        that.$router.push({path:'/forumDetail',query: {id: item.id}})
      },
      editor() {
        let that = this;
        that.show = true;
      },
      beforeClose(action, done) {
        let that = this;
        if(action === 'confirm'){
          if(!that.title){
            that.$toast('标题不能为空！')
            done(false)
            return false;
          }else if (!that.content) {
            that.$toast('内容不能为空！')
            done(false)
            return false;
          }
          let formData = {
            title: that.title,
            content: that.content,
            pic: that.pic,
            stname: that.stname
          };
          console.log('formData',formData)
          that.$api.post('/mobile/Stmsg/ftadd', {'user':formData}, r => {
            console.log('发帖成功',r)
            if(r.code == 1){
              setTimeout(done, 500);
              this.$router.replace({
                path: '/refresh',
                query: {
                  id: r.data.hfid,
                  t: Date.now()
                }
              })
            }
          });

        } else if(action === 'cancel') {
          done() //关闭
        }
      }
    }
  }
</script>

<style scoped>

  .add-wrapper {
    position: fixed;
    bottom: 100px;
    right: 12px;
  }
</style>
